/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.BaoCaoDoanhThuDTO;

/**
 *
 * @author tuan
 */
public class BaoCaoDoanhThuDAO {
    private static final String MaBaoCaoDoanhThu = "MaBCDT00";
    private static final int lengthMa = 8;
    
    public BaoCaoDoanhThuDAO() {
    }
    
    public String getNewMaBaoCaoDoanhThu() throws SQLException {
        String MaBCDT = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllBaoCaoDoanhThuFromDataBase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MABCDT").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaBCDT = MaBCDT.concat(MaBaoCaoDoanhThu + maxMa);
        return MaBCDT;
    }
    
    public BaoCaoDoanhThuDTO getBaoCaoDoanhThuByMaBCDT(String MaBCDT) throws SQLException {
        Object[] objs = { MaBCDT };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetBaoCaoDoanhThuFromDataBase( ? )", objs);
        BaoCaoDoanhThuDTO baocaoDT = new BaoCaoDoanhThuDTO();
        while (result.next()) {
            baocaoDT.setMaBCDT(result.getString("MABCDT"));
            baocaoDT.setThang(result.getDate("THANG"));
        }
        return baocaoDT;
    }
    
    public ArrayList<BaoCaoDoanhThuDTO> getBaoCaoDoanhThuInTime(Date timeStart, Date timeEnd) throws SQLException {
        ArrayList<BaoCaoDoanhThuDTO> listBaoCao = new ArrayList<>();
        Object[] objs = { timeStart, timeEnd };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetBaoCaoDoanhThuInTimeFromDataBase( ?, ? )", objs);
        BaoCaoDoanhThuDTO baocaoDT = new BaoCaoDoanhThuDTO();
        while (result.next()) {
            baocaoDT.setMaBCDT(result.getString("MABCDT"));
            baocaoDT.setThang(result.getDate("THANG"));
            listBaoCao.add(baocaoDT);
        }
        return listBaoCao;
    }
    
    public int updateBaoCaoDoanhThu(BaoCaoDoanhThuDTO baocaoDT) {
        Object[] objs = { baocaoDT.getMaBCDT(), baocaoDT.getThang() };
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateBaoCaoDoanhThuToDataBase(?, ?)", objs);
    }
    
    public int deleteBaoCaoDoanhThuByMaBC(String MaBC) {
        Object[] objs = { MaBC };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteBaoCaoDoanhThuFromDataBase( ? )", objs);
    }
    
    public int deleteBaoCaoDoanhThuByThang(Date thang) {
        Object[] objs = { thang };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteBaoCaoDoanhThuInMonthFromDataBase( ? )", objs);
    }
    
    public int insertBaoCaoDoanhThu(BaoCaoDoanhThuDTO baocaoDT) {
        Object[] objs = { baocaoDT.getMaBCDT(), baocaoDT.getThang() };
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertBaoCaoDoanhThuToDataBase(?, ?)", objs);
    }    
}
